package me.sluijsens.AntiEnderman;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

public class cmdExecutor implements CommandExecutor {
	private AntiEnderman plugin = null;
	private ConfigHandler conf = null;

	public List<World> worlds;
	public List<String> world_names;

	boolean truefalse = true;

	// Declare logger
	Logger log = Logger.getLogger("Minecraft");

	public cmdExecutor(AntiEnderman instance) {
		plugin = instance;

		worlds = plugin.getServer().getWorlds();
		world_names = new ArrayList<String>();

		for (int i = 0; i < worlds.size(); i++) {
			world_names.add(worlds.get(i).getName());
		}
	}

	@SuppressWarnings("static-access")
	@Override
	public boolean onCommand(CommandSender sender, Command command,
			String label, String[] args) {
		Player player = (Player) sender;
		conf = plugin.getConf();
		truefalse = true;

		if (args.length < 1) {
			if (player.isOp()
					|| plugin.getServer().getPluginManager().getPlugin("Vault") != null
					&& plugin.permission.has(player, "ae.help")) {
				player.sendMessage(plugin.tagChat + "Commands:");
				player.sendMessage(plugin.tagChat + ChatColor.RED + "[]"
						+ ChatColor.WHITE + " - Optional, " + ChatColor.RED
						+ "<>" + ChatColor.WHITE + " - Required");
				player.sendMessage(plugin.tagChat + ChatColor.RED
						+ "/ae [help]" + ChatColor.WHITE + " Shows this help.");
				player.sendMessage(plugin.tagChat
						+ ChatColor.RED
						+ "/ae spawn [world] <true|false>"
						+ ChatColor.WHITE
						+ " Configure if enderman may spawn or not and in which world.");
				player.sendMessage(plugin.tagChat
						+ ChatColor.RED
						+ "/ae place [world] <true|false>"
						+ ChatColor.WHITE
						+ " Configure if enderman may place blocks or not and in which world.");
				player.sendMessage(plugin.tagChat
						+ ChatColor.RED
						+ "/ae break [world] <true|false>"
						+ ChatColor.WHITE
						+ " Configure if enderman may break blocks or not and in which world.");
				player.sendMessage(plugin.tagChat
						+ ChatColor.RED
						+ "/ae teleport|tp|tele [world] <true|false>"
						+ ChatColor.WHITE
						+ " Configure if enderman may teleport or not and in which world.");
				player.sendMessage(plugin.tagChat + ChatColor.RED
						+ "/ae worlds" + ChatColor.WHITE
						+ " Show all the world on this server");
				player.sendMessage(plugin.tagChat + ChatColor.RED
						+ "/ae clear" + ChatColor.WHITE
						+ " Clear all enderman from world.");
			} else {
				player.sendMessage(plugin.tagChat
						+ "You do not have permission to use this command.");
			}
			return true;
		} else if (args[0].equalsIgnoreCase("worlds")) {
			if (player.isOp()
					|| plugin.getServer().getPluginManager().getPlugin("Vault") != null
					&& plugin.permission.has(player, "ae.worlds")) {
				player.sendMessage(plugin.tagChat + "Worlds:");

				for (int i = 0; i < world_names.size(); i++) {
					player.sendMessage("- " + world_names.get(i));
				}
			} else {
				player.sendMessage(plugin.tagChat
						+ "You do not have permission to use this command.");
			}
			return true;
		}else if(args[0].equalsIgnoreCase("clear")){
			if (player.isOp()
					|| plugin.getServer().getPluginManager().getPlugin("Vault") != null
					&& plugin.permission.has(player, "ae.clear")) {
				for(Entity e : player.getWorld().getEntities()){
					if(e instanceof Enderman){
						((Enderman) e).setHealth(0);
					}
				}
				player.sendMessage(plugin.tagChat + "Cleared world of enderman.");
			} else {
				player.sendMessage(plugin.tagChat
						+ "You do not have permission to use this command.");
			}
			return true;
		} else {
			return execute_command(player, args);
		}
	}

	@SuppressWarnings("static-access")
	private boolean execute_command(Player player, String[] args) {
		String cmd = args[0];
		if (cmd.equalsIgnoreCase("tp") || cmd.equalsIgnoreCase("tele")) {
			cmd = "teleport";
		}
		cmd.toLowerCase();

		if(player.isOp() || plugin.permission.has(player, "ae." + cmd)) {
			if (args.length <= 2) {
				if ((args.length == 2)
						&& (args[1].equalsIgnoreCase("true") || args[1]
								.equalsIgnoreCase("false"))) {
					
					if (args[1].equalsIgnoreCase("false")) {
						truefalse = false;
					}

					conf.set("block.enderman." + cmd, truefalse);
					player.sendMessage(plugin.tagChat + "Enderman "
							+ ChatColor.RED + cmd + ChatColor.WHITE
							+ " set to " + ChatColor.RED + args[1]
							+ ChatColor.WHITE + ".");
				} else {
					player.sendMessage(plugin.tagChat + ChatColor.RED + "/ae "
							+ cmd + " true|false");
					return false;
				}
			} else if (world_names.contains(args[1])) {
				if ((args.length > 2)
						&& (args[2].equalsIgnoreCase("true") || args[2]
								.equalsIgnoreCase("false"))) {

					if (args[2].equalsIgnoreCase("false")) {
						truefalse = false;
					}

					conf.set("block." + args[1] + ".enderman." + cmd, truefalse);
					player.sendMessage(plugin.tagChat + "Enderman "
							+ ChatColor.RED + cmd + ChatColor.WHITE
							+ " set to " + ChatColor.RED + args[2]
							+ ChatColor.WHITE + " in world " + ChatColor.RED
							+ args[1] + ChatColor.WHITE + ".");
				} else {
					player.sendMessage(plugin.tagChat + ChatColor.RED + "/ae "
							+ cmd + " world true|false");
					return false;
				}
			} else {
				player.sendMessage(plugin.tagChat
						+ "That world does not exist. Use /ae worlds to view existing worlds.");
				return false;
			}

			conf.save();
		} else {
			player.sendMessage(plugin.tagChat
					+ "You do not have permission to use this command.");
			return false;
		}
		return true;
	}
}
